Introducing TensorFlow.js: Machine Learning in Javascript


TensorFlow.js, an open-source library entirely in the browser, using Javascript and a high-level layers API.

In-Browser ML

Running machine learning programs entirely client-side in the browser, like interactive ML!
If you’re watching the livestream for the TensorFlow Developer Summit, during the TensorFlow.js talk you’ll find a demo where @dsmilkov and @nsthorat train a model to control a PAC-MAN game using computer vision and a webcam, entirely in the browser.
You can try it out yourself, too, with the link below — and find the source in the examples folder.



Turn your webcam into a controller for PAC-MAN using a Neural Network .

If you’d like to try another game, give the Emoji Scavenger Hunt a whirl — this time, from a browser on your mobile phone.

The Emoji Scavenger Hunt is another fun example of an application built using TensorFlow.js.
Try it using your phone, and find the source
here .

ML running in the browser means that from a user’s perspective, there’s no need to install any libraries or drivers.
Just open a webpage, and your program is ready to run.
In addition, it’s ready to run with GPU acceleration.
TensorFlow.js automatically supports WebGL, and will accelerate your code behind the scenes when a GPU is available.
Users may also open your webpage from a mobile device, in which case your model can take advantage of sensor data, say from a gyroscope or accelerometer.
Finally, all data stays on the client, making TensorFlow.js useful for low-latency inference, as well as for privacy preserving applications.

What can you do with TensorFlow.js?

If you’re developing with TensorFlow.js, here are three workflows you can consider.

Let’s see some code

If you like, you can head directly to the samples or tutorials to get started.
These show how-to export a model defined in Python for inference in the browser, as well as how to define and train models entirely in Javascript.
As a quick preview, here’s a snippet of code that defines a neural network to classify flowers, much like on the getting started guide on TensorFlow.org.
Here, we’ll define a model using a stack of layers.

import * as tf from ‘@tensorflow/tfjs’;
const model = tf.sequential();
model.add(tf.layers.dense({inputShape: [4], units: 100}));
model.add(tf.layers.dense({units: 4}));
model.compile({loss: ‘categoricalCrossentropy’, optimizer: ‘sgd’});

The layers API we’re using here supports all of the Keras layers found in the examples directory (including Dense, CNN, LSTM, and so on).
We can then train our model using the same Keras-compatible API with a method call:

await model.fit(
  xData, yData, {
    batchSize: batchSize,
    epochs: epochs
});

The model is now ready to use to make predictions:

// Get measurements for a new flower to generate a prediction
// The first argument is the data, and the second is the shape.
const inputData = tf.tensor2d([[4.8, 3.0, 1.4, 0.1]], [1, 4]);

// Get the highest confidence prediction from our model
const result = model.predict(inputData);
const winner = irisClasses[result.argMax().dataSync()[0]];

// Display the winner
console.log(winner);

TensorFlow.js also includes a low-level API (previously deeplearn.js) and support for Eager execution.
You can learn more about these by watching the talk at the TensorFlow Developer Summit.



An overview of TensorFlow.js APIs.
TensorFlow.js is powered by WebGL and provides a high-level layers API for defining models, and a low-level API for linear algebra and automatic differentiation.
TensorFlow.js supports importing TensorFlow SavedModels and Keras models.

How does TensorFlow.js relate to deeplearn.js?

Good question! TensorFlow.js, an ecosystem of JavaScript tools for machine learning, is the successor to deeplearn.js which is now called TensorFlow.js Core.
TensorFlow.js also includes a Layers API, which is a higher level library for building machine learning models that uses Core, as well as tools for automatically porting TensorFlow SavedModels and Keras hdf5 models.
For answers to more questions like this, check out the FAQ.

Where’s the best place to learn more?

To learn more about TensorFlow.js, visit the project homepage, check out the tutorials, and try the examples.
You can also watch the talk from the 2018 TensorFlow Developer Summit, and follow TensorFlow on Twitter.